package com.thinkgem.jeesite.modules.transfer.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.transfer.entity.Transfer;
import com.thinkgem.jeesite.modules.transfer.service.TransferService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/transfer/transferview")
public class TransferView extends BaseController {

    @Autowired
    private TransferService transferService;

    /**
     *
     * @param transfer
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("transfer:transfer:view")
    @RequestMapping(value = "transferassign")
    public String transferassign(Transfer transfer, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Map<String, Object> assignMap = Maps.newHashMap();
        List<Transfer> transferList = transferService.getAllTransferAssign(transfer);
        String assignSum = transferList.get(0).getCount();
        transfer.setUserType("大专院校");
        List<Transfer> transferSchoolList = transferService.getAllTransferAssign(transfer);
        String schoolAssignSum = transferSchoolList.get(0).getCount();
        assignMap.put("assignsum",assignSum);
        assignMap.put("schoolassignsum",schoolAssignSum);
        mapList.add(assignMap);
        model.addAttribute("mapList", mapList);
        return "modules/transfer/transferAssign";
    }

    /**
     * 专利转让年度趋势
     * @param transfer
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("transfer:transfer:view")
    @RequestMapping(value = "yearassign")
    public String yearassign(Transfer transfer, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Map<String, Object> map1 = Maps.newHashMap();
        String dateStr = new DateTime().toString("yyyy");//系统时间
        int datetime = 0;
        for (int i=0;i<10;i++){
            int s = i+1;
            transfer = new Transfer();
            transfer.setDatetime(dateStr);
            List<Transfer> countUserYear = transferService.getAssignYear(transfer);
            map1.put("assignCount"+s,countUserYear.get(0).getCount());
            transfer.setUserType("大专院校");
            List<Transfer> universitiesYearSum = transferService.getAssignYear(transfer);
            map1.put("Universities"+s,universitiesYearSum.get(0).getCount());
            map1.put("year"+s,dateStr);

            datetime = Integer.parseInt(dateStr);
            datetime -=1;
            dateStr = String.valueOf(datetime);
        }
        mapList.add(map1);
        model.addAttribute("mapList", mapList);
        return "modules/transfer/yearassign";
    }

    /**
     * 高校专利转让前十
     * @param transfer
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("transfer:transfer:view")
    @RequestMapping(value = "schoolassigntop")
    public String schoolassigntop(Transfer transfer, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Map<String, Object> map1 = Maps.newHashMap();
        List<Transfer> assignSchoolList = transferService.getSchoolAssign(transfer);
        for(int i=0;i<assignSchoolList.size();i++){
            int s = i+1;
            String UserName = assignSchoolList.get(i).getPatentUser();
            String count = assignSchoolList.get(i).getCount();
            map1.put("schoolName"+s,UserName);
            map1.put("schoolCount"+s,count);
//            System.out.println("排名第"+s+"的高校是"+UserName+",总共转让"+count+"件");
        }
        if(assignSchoolList.size()<10){
            for (int j=1;j<=10-assignSchoolList.size();j++){
                int z = j+assignSchoolList.size();
                String UserName = " ";
                String count = "0";
                map1.put("schoolName"+z,UserName);
                map1.put("schoolCount"+z,count);
            }
        }
        mapList.add(map1);
        model.addAttribute("mapList", mapList);
        return "modules/transfer/schoolassigntop";
    }
    /**
     * 企业专利受让前十
     * @param transfer
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("transfer:transfer:view")
    @RequestMapping(value = "receptorassigntop")
    public String receptorassigntop(Transfer transfer, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Map<String, Object> map1 = Maps.newHashMap();
        List<Transfer> getReceptorAssigList = transferService.getReceptorAssign(transfer);
        for(int i=0;i<getReceptorAssigList.size();i++){
            int s = i+1;
            String UserName = getReceptorAssigList.get(i).getTransferReceptor();
            String count = getReceptorAssigList.get(i).getCount();
            map1.put("Name"+s,UserName);
            map1.put("Count"+s,count);
//            System.out.println("排名第"+s+"的受让人是"+UserName+",总共受让专利"+count+"件");
        }
        if(getReceptorAssigList.size()<10){
            for (int j=1;j<=10-getReceptorAssigList.size();j++){
                int z = j+getReceptorAssigList.size();
                String UserName = " ";
                String count = "0";
                map1.put("Name"+z,UserName);
                map1.put("Count"+z,count);
            }
        }
        mapList.add(map1);
        model.addAttribute("mapList", mapList);
        return "modules/transfer/receptorassigntop";
    }
}
